package fr.sedoo.commons.client.faq.activity;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import fr.sedoo.commons.client.event.ActionEventConstants;
import fr.sedoo.commons.client.event.ActionStartEvent;
import fr.sedoo.commons.client.event.ActivityStartEvent;
import fr.sedoo.commons.client.event.BackEvent;
import fr.sedoo.commons.client.event.NotificationEvent;
import fr.sedoo.commons.client.faq.mvp.FaqClientFactory;
import fr.sedoo.commons.client.faq.place.FaqEditPlace;
import fr.sedoo.commons.client.faq.service.FaqService;
import fr.sedoo.commons.client.faq.service.FaqServiceAsync;
import fr.sedoo.commons.client.faq.ui.FaqEditView;
import fr.sedoo.commons.client.message.CommonMessages;
import fr.sedoo.commons.client.mvp.AuthenticatedClientFactory;
import fr.sedoo.commons.client.mvp.activity.AdministrationActivity;
import fr.sedoo.commons.client.service.DefaultAbstractCallBack;
import fr.sedoo.commons.client.util.StringUtil;
import fr.sedoo.commons.shared.domain.faq.QuestionAndAnswer;

public class FaqEditActivity extends AdministrationActivity implements FaqEditView.Presenter  {

	private FaqEditView editView;
	private String uuid="";
	private FaqEditPlace place;
	
	private static final FaqServiceAsync FAQ_SERVICE = GWT.create(FaqService.class);

    public FaqEditActivity(FaqEditPlace place, AuthenticatedClientFactory clientFactory) {
    	super(clientFactory, place);
		this.place = place;
    	uuid = place.getUuid(); 
    }

    /**
     * Invoked by the ActivityManager to start a new Activity
     */
    @Override
    public void start(final AcceptsOneWidget containerWidget, EventBus eventBus) {
    	if (isValidUser() == false)
		{
			goToLoginPlace();
			return;
		}
		editView = ((FaqClientFactory) clientFactory).getFaqEditView();
		editView.setPresenter(this);
    	editView.reset();
    	eventBus.fireEvent(new ActivityStartEvent(this));
    	if (StringUtil.isEmpty(uuid))
    	{
    		//Creation
    		editView.setContent(new HashMap<String, QuestionAndAnswer>(), clientFactory.getLanguages());
    		containerWidget.setWidget(editView.asWidget());
    	}
    	else
    	{
    		ActionStartEvent e = new ActionStartEvent(CommonMessages.INSTANCE.loading(), ActionEventConstants.BASIC_LOADING_EVENT, true);
        	eventBus.fireEvent(e);
        	
        	FAQ_SERVICE.getFaqContentByUuid(uuid, new DefaultAbstractCallBack<HashMap<String, QuestionAndAnswer>>(e, eventBus) {
    			@Override
    			public void onSuccess(HashMap<String, QuestionAndAnswer> result) {
    				super.onSuccess(result);
    				editView.setContent(result, clientFactory.getLanguages());
    				containerWidget.setWidget(editView.asWidget());
    			}
    		});
    	}
    }


	@Override
	public void save(final HashMap<String, QuestionAndAnswer> content) 
	{
		EventBus eventBus = clientFactory.getEventBus();
		ActionStartEvent e = new ActionStartEvent(CommonMessages.INSTANCE.saving(), ActionEventConstants.BASIC_SAVING_EVENT, true);
    	eventBus.fireEvent(e);
    	
    	FAQ_SERVICE.saveFaq(uuid, content,  new DefaultAbstractCallBack<Void>(e, eventBus) {
			@Override
			public void onSuccess(Void result) {
				super.onSuccess(result);
				clientFactory.getEventBus().fireEvent(new NotificationEvent(CommonMessages.INSTANCE.savedElement()));
			}
		});
	}

	@Override
	public void back() 
	{
		clientFactory.getEventBus().fireEvent(new BackEvent());
	}
	public FaqEditPlace getPlace() {
		return place;
	}
   
}